
#include <kmath.h>


double pow(double x, int y){

  if (y == 0) return 1;
  if (y < 0) return 0;

  double res = x;
  int i = 1;
  for ( ; i < y; i++){
    res *= (double)y;
  }

  return res;

}

long fact(int x)
{
  long result = 1; //set to one for first iteration of while loop
  
  while (x >= 1)
  {
    result *= x;
    x--;
  }

  return result;
}

double cos(double x)
{
  //double x = 3.141592653;
  double tol = 0.0001; //calculate the cos of x to this tolerance
  double n = 1, cos = 1, term = 1, alt = -1;

  while (term > tol)
  {
    term *= pow(x,2)/n/(n+1);
    cos += alt*term;
    alt *= -1;
    n += 2;
  }
  
  return cos;
}

double sin(double x)
{
  double tol = 0.0001; //calculate the sin of x to this tolerance
  double n = 1, sin = 1, term = 1, alt = -1;

  while (term > tol)
  {
    term *= pow(x,n)/fact((int)n);
    sin += alt*term;
    alt *= -1;
    n += 2;
  }
  return sin;
}
